package ttt.core;

import com.sun.rmi.rmid.ExecPermission;
import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("desk")
public class Desk {
	public static final int NONE = 0;

	public static final int CROSS = -1;

	public static final int SPOT = -2;

	private int[][] matrix;

	public Desk(int[][] matrix) {
		this.matrix = matrix;
	}

	public int[][] getMatrix() {
		return matrix;
	}

	public boolean makeTurn(Turn turn, int type) {
		try {
			int c = matrix[turn.getX()][turn.getY()];
			if (c == NONE) {
				matrix[turn.getX()][turn.getY()] = type;
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}

	public String toHTML() {
		String s = "<html>";

		s += "  ";
		for (int y = 0; y < matrix[0].length; y++) {
			s += (y + 1) + " ";
		}
		s += "<br>";
		for (int x = 0; x < matrix.length; x++) {

			for (int y = 0; y < matrix[x].length; y++) {
				int c = matrix[x][y];
				if (c == CROSS) {
					s += "<font color=red>X</font> ";
				} else if (c == SPOT) {
					s += "<font color=blue>O</font> ";
				} else {
					s += "<font color=green>.</font> ";
				}

			}
			s += (x+1) + "<br>";
		}
		s += "</html>";
		return s;

	}

	public String toString() {
		String s = "";

		for (int x = 0; x < matrix.length; x++) {
			for (int y = 0; y < matrix[x].length; y++) {
				s += matrix[x][y] + " ";
			}
			s += "\n";
		}
		return s;

	}
}
